﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI;
using Antu.WebControls;
using Antu.Core.Components;
using Antu.Core.Services;
using System.Web.UI.WebControls;
using EluZSProvider;
using System.Web;
using System.Data;

namespace WebSys.UI.Control.ZhengShou
{
    public class ZSPG_ZGFBCJFHAddControl : MPBasicTemplate
    {
        EluZSDataProvider _EluZSDataProvider = (EluZSDataProvider)ProviderService.ProviderServiceMange.GetProvider("EluZSProvider");
        public string userid = string.Empty;
        protected override void OnLoad(EventArgs e)
        {
            if (HttpContext.Current.Session["UserID"] == null)
            {
                Antu.Utilities.UtilitiesPage.AlertDialogClear("登录超时，请重新登录！", "window.parent.location.href('../login.aspx');");
            }
            else
            {
                userid = HttpContext.Current.Session["UserID"].ToString();
            }
            base.OnLoad(e);
            MPPersistence Persistence1 = (MPPersistence)FindControl("Persistence1");
            Panel panel1 = (Panel)FindControl("panel1");
            if (!base.Page.IsPostBack)
            {
                Persistence1.Table = "ZS_ZZBCJFENHU";
                Persistence1.Key = "ID";
                Persistence1.KeyValue = HttpContext.Current.Request["ID"];
                Persistence1.ConnStr = _EluZSDataProvider.GetConnectionString();
            }
            Persistence1.BindControl = panel1;
            BindData();
            IsLookPerson();
        }

        private void IsLookPerson()
        {
            string sql = "Select rolename from zs_role where id in (Select roleid from zs_user_in_role where userid = '" + userid + "')";
            DataTable dt = _EluZSDataProvider.GetTable(sql);
            if (dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    if (dt.Rows[i]["rolename"].ToString().Equals("查看人员"))
                    {
                        Button btn_Insert = (Button)FindControl("btn_Insert");
                        btn_Insert.Visible = false;
                    }
                }
            }
        }

        protected override void OnPreRender(EventArgs e) {
            base.OnPreRender(e);

            TextBox 房屋坐落 = (TextBox)FindControl("房屋坐落");
            TextBox 产权人 = (TextBox)FindControl("产权人");
            TextBox 承租人 = (TextBox)FindControl("承租人");
            TextBox 房屋权证编号 = (TextBox)FindControl("房屋权证编号");
            TextBox 房屋面积 = (TextBox)FindControl("房屋面积");

            TextBox 土地权证编号 = (TextBox)FindControl("土地权证编号");
            TextBox 土地面积 = (TextBox)FindControl("土地面积");
            TextBox 丘号 = (TextBox)FindControl("丘号");
            TextBox 区位级别 = (TextBox)FindControl("区位级别");
            TextBox txt所在层次 = (TextBox)FindControl("txtZZ所在层次");

            TextBox txtPID = (TextBox)FindControl("txtPID");

            string PID = HttpContext.Current.Request["PID"];
            string ZSPID = HttpContext.Current.Request["ZSPID"];

            if (!string.IsNullOrEmpty(PID)){ txtPID.Text = PID; }
            if (!string.IsNullOrEmpty(ZSPID)) {
                EluZSProvider.ZSPERSON zsperson = _EluZSDataProvider.GetZSPERSON(ZSPID);
                房屋坐落.Text = zsperson.房屋坐落;
                产权人.Text = zsperson.产权人;
                承租人.Text = zsperson.承租人;
                土地面积.Text = zsperson.土地分摊面积;
                丘号.Text = zsperson.丘权号;
                土地权证编号.Text = zsperson.土地权证编号;
                //txt所在层次.Text = zsperson.所属层次;
                房屋权证编号.Text = zsperson.房屋权证编号;
                房屋面积.Text = zsperson.房屋建筑面积;
                区位级别.Text = zsperson.征收区位等级;
                string id = HttpContext.Current.Request["ID"];
                if (!string.IsNullOrEmpty(ZSPID) && string.IsNullOrEmpty(id))
                {
                    txt所在层次.Text = zsperson.所属层次;
                }
                bool ifqsxy = _EluZSDataProvider.GetBZSRXY(ZSPID);
                if (ifqsxy)
                {
                    Button btn_Insert = (Button)FindControl("btn_Insert");
                    Button btnUpload = (Button)FindControl("btnUpload");
                    btn_Insert.Visible = false;
                    btnUpload.Text = "附件列表";
                    base.Page.ClientScript.RegisterStartupScript(this.GetType(), "displaybutton", "<script>displaybutton();</script>");
                }

                string sql = "Select 是否退回 from zs_hbhxy where PERSONID = '" + ZSPID + "'";
                DataTable dt = _EluZSDataProvider.GetTable(sql);
                if (dt.Rows.Count > 0)
                {
                    if (dt.Rows[0]["是否退回"].ToString() == "1")
                    {
                        Button btn_Insert = (Button)FindControl("btn_Insert");
                        btn_Insert.Visible = true;
                        btn_Insert.Enabled = true;
                    }

                }
            }
            
        }
        protected void BindData() {
            Button btn_Insert = (Button)FindControl("btn_Insert");
            Button btnUpload = (Button)FindControl("btnUpload");
            btn_Insert.Click += new EventHandler(btn_Insert_Click);
            btnUpload.Click += new EventHandler(btnUpload_Click);
        }

        protected void btnUpload_Click(object sender, EventArgs e)
        {
            MPPersistence Persistence1 = (MPPersistence)FindControl("Persistence1");
            if (!string.IsNullOrEmpty(Persistence1.KeyValue)) {
                string strScript = string.Format("<script>OpenFileUpload('{0}','{1}','{2}');</script>",
                Persistence1.KeyValue, base.Page.Request["ID"].Trim(), base.Page.Request["PID"].Trim());
                Page.ClientScript.RegisterStartupScript(this.GetType(), "opennew", strScript);
            }
        }

        protected void btn_Insert_Click(object sender, EventArgs e)
        {
            TextBox txtZSPID = (TextBox)FindControl("txtZSPID");
            MPPersistence Persistence1 = (MPPersistence)FindControl("Persistence1");
            Persistence1.Update();
            string strPID = base.Page.Request["PID"];
            if (_EluZSDataProvider.CkFileCount(strPID, "11")) { _EluZSDataProvider.UpdateFileCS(strPID, "11"); }
            HttpContext.Current.Response.Redirect("ZSPG_ZGFBCJFHAdd.aspx?ID=" + Persistence1.KeyValue + "&PID=" + base.Page.Request["PID"] + "&ZSPID=" + txtZSPID.Text);
        }
    }
}


